package com.kmxd.ams.app.jy;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kmxd.ams.client.jy.IAuthDetailsSvc;
import com.kmxd.ams.client.jy.dto.AuthDetailsAddDTO;
import com.kmxd.ams.client.jy.dto.AuthDetailsDeleteDTO;
import com.kmxd.ams.client.jy.dto.AuthDetailsEditDTO;
import com.kmxd.ams.client.jy.dto.AuthDetailsQueryDTO;
import com.kmxd.ams.client.jy.vo.AuthDetailsListVO;
import com.kmxd.ams.client.jy.vo.AuthDetailsVO;
import com.kmxd.ams.core.common.constant.ErrorCode;
import com.kmxd.ams.core.common.query.QueryGen;
import com.kmxd.ams.core.exception.BizTipException;
import com.kmxd.ams.core.util.IdUtil;
import com.kmxd.ams.core.util.SecurityUtil;
import com.kmxd.ams.infra.jy.entity.AuthDetails;
import com.kmxd.ams.infra.jy.mapper.AuthDetailsMapper;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 授权档案明细 服务实现类
 *
 * @author ZuoLG
 * @since 2023-11-30
 */
@Service
@AllArgsConstructor
public class AuthDetailsSvcImpl extends ServiceImpl<AuthDetailsMapper, AuthDetails>
    implements IAuthDetailsSvc {

  @Override
  public Page<AuthDetailsListVO> selectPage(Page reqPage, AuthDetailsQueryDTO req) {
    LambdaQueryWrapper<AuthDetails> queryWrapper =
        QueryGen.init(new QueryWrapper<AuthDetails>(), req).lambda();
    Page<AuthDetails> page = this.baseMapper.selectPage(reqPage, queryWrapper);
    Page<AuthDetailsListVO> voPage = BeanUtil.copyProperties(page, Page.class);
    voPage.setRecords(BeanUtil.copyToList(page.getRecords(), AuthDetailsListVO.class));
    return voPage;
  }

  @Override
  public List<AuthDetailsListVO> selectList(AuthDetailsQueryDTO req) {
    LambdaQueryWrapper<AuthDetails> queryWrapper =
        QueryGen.init(new QueryWrapper<AuthDetails>(), req).lambda();
    List<AuthDetails> list = this.baseMapper.selectList(queryWrapper);
    return BeanUtil.copyToList(list, AuthDetailsListVO.class);
  }

  @Override
  public List<String> selectByIds(AuthDetailsQueryDTO req) {
    LambdaQueryWrapper<AuthDetails> queryWrapper = Wrappers.lambdaQuery();
    req.setUserId(ObjUtil.isEmpty(req.getUserId()) ? SecurityUtil.getUserId() : req.getUserId());
    queryWrapper.eq(AuthDetails::getUserId, req.getUserId());
    if (ObjUtil.isNotEmpty(req.getArchivesId())) {
      queryWrapper.eq(AuthDetails::getArchivesId, req.getArchivesId());
    }
    queryWrapper.eq(ObjUtil.isNotEmpty(req.getArchivesId()),AuthDetails::getArchivesId, req.getArchivesId());
    queryWrapper.eq(ObjUtil.isNotEmpty(req.getAuthType()),AuthDetails::getAuthType, req.getAuthType());
    queryWrapper.eq(ObjUtil.isNotEmpty(req.getStatus()),AuthDetails::getStatus, req.getStatus());
    List<AuthDetails> list = this.baseMapper.selectList(queryWrapper);
    List<String> ids =
        list.stream().map(AuthDetails::getObjId).map(Object::toString).collect(Collectors.toList());
    return ids;
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void add(AuthDetailsAddDTO req) {
    if (ObjUtil.isEmpty(req.getObjIds())) {
      throw BizTipException.instance(ErrorCode.VALIDATE_NOT_NULL, "操作对象不能为空！");
    }
    List<Long> archIds = IdUtil.split(req.getObjIds(), Long.class);
    List<AuthDetails> entities =
        archIds.stream()
            .map(
                item -> {
                  AuthDetails entity = BeanUtil.copyProperties(req, AuthDetails.class);
                  entity.setObjId(item);
                  return entity;
                })
            .collect(Collectors.toList());
    this.saveOrUpdateBatch(entities);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void edit(AuthDetailsEditDTO req) {
    AuthDetails entity = BeanUtil.copyProperties(req, AuthDetails.class);
    this.baseMapper.updateById(entity);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void remove(AuthDetailsDeleteDTO req) {
    if (ObjUtil.isEmpty(req.getIds())) {
      throw BizTipException.instance(ErrorCode.VALIDATE_NOT_NULL, "操作对象不能为空！");
    }
    Set<Long> idList = IdUtil.split(req.getIds());
    LambdaQueryWrapper<AuthDetails> queryWrapper = Wrappers.lambdaQuery();
    queryWrapper.eq(AuthDetails::getUserId, req.getUserId());
    queryWrapper.in(AuthDetails::getObjId, idList);
    queryWrapper.eq(AuthDetails::getArchivesId, req.getArchivesId());
    queryWrapper.eq(AuthDetails::getAuthType, req.getAuthType());

    this.baseMapper.delete(queryWrapper);
  }

  @Override
  public AuthDetailsVO view(Long id) {
    AuthDetails entity = this.baseMapper.selectById(id);
    return BeanUtil.copyProperties(entity, AuthDetailsVO.class);
  }
}
